#pragma once

#include "const.h"
#include "data.h"
#include "Singleton.h"
#include "ConfigManager.h"
#include "message.grpc.pb.h"
#include "message.pb.h"

#include <json/json.h>
#include <json/value.h>
#include <json/reader.h>
#include <grpc++/grpc++.h>
#include <queue>

class ChatConPool
{
public:
	ChatConPool(size_t poolSize, std::string host, std::string port);

	~ChatConPool();

	void Close();

	std::unique_ptr<message::ChatService::Stub> getConnection();

	void returnConnection(std::unique_ptr<message::ChatService::Stub> con);

private:
	std::queue<std::unique_ptr<message::ChatService::Stub>> m_connections;
	size_t m_poolSize;
	std::atomic_bool m_b_stop;
	std::mutex m_mutex;
	std::condition_variable m_cond;
	std::string m_host;
	std::string m_port;
};

class ChatGrpcClient : public Singleton<ChatGrpcClient>
{
	friend class Singleton<ChatGrpcClient>;

public:
	bool getBaeInfo(std::string base_key, int uid, std::shared_ptr<UserInfo>& userinfo);

	message::AddFriendRsp notifyAddFriend(std::string server_ip, const message::AddFriendReq& req);
	message::AuthFriendRsp notifyAuthFriend(std::string server_ip, const message::AuthFriendReq& req);
	message::TextChatMsgRsp notifyTextChatMsg(std::string server_ip, const message::TextChatMsgReq& req, const Json::Value& rtvalue);

private:
	ChatGrpcClient();

private:
	std::unordered_map<std::string, std::unique_ptr<ChatConPool>> m_pools;	// 连接名(IP)与连接池之间的映射
};

